﻿var itemFormVue = new Vue({
    el: "#itemForm",
    data: {
        isNewItem: true,
        item: {
            Name: "",
            EquipmentTypeID: 0,
            IsDisable: 0
        },
        typeDictionary: {},
        isDisableDictionary: {}
    },
    methods: {
        SaveItem: function (event) {
            event.preventDefault();
            SaveMaintainItem(this);
        },
        UpdateItem: function (event) {
            event.preventDefault();
            UpdateMaintainItem(this);
        }
    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        this.typeDictionary = GlobalDictionary.GetEquipmentTypeDictionary();
        this.isDisableDictionary = GlobalDictionary.GetTrueOrFalseDictionary();

        var isNew = GetQueryString("IsNew");
        var itemID = GetQueryString("ItemID");
        if (isNew == "true") {
            this.isNewItem = true;
        } else {
            this.isNewItem = false;
            GetMaintainItem(this, itemID);
        }
    },
    watch: {
        item: {
            handler: function (val, oVal) {
                if (val.Name.length > 0)
                    CheckMaintainItemName(this);
                if (val.EquipmentTypeID > 0)
                    CheckEquipmentType(this);
            },
            deep: true
        }
    }
});

/**
 * 查询保养项目详细信息
 */
function GetMaintainItem(vue, itemID) {
    parent.ShowAzLoading();
    $.ajax({
        type: "GET",
        url: "/Maintain/GetMaintainItemByID?ItemID=" + itemID + "&v=" + Math.random(),
        success: function (res) {
            if (res.status == "Success") {
                vue.item = res.data;
            } else {
                toastr.error(res.message);
            }
            parent.HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            parent.HideAzLoading();
            toastr.error("加载保养项目信息失败，请稍候再试Failed to load maintenance item information. Please try again later。");
        }
    });
}

/**
 * 保存保养项目信息
 */
function SaveMaintainItem(vue) {
    if (CheckMaintainItem(vue)) {
        parent.ShowAzLoading();
        $("button").attr("disabled", true);
        $.ajax({
            type: "POST",
            url: "/Maintain/SaveMaintainItem",
            data: { item: vue.item },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("保存保养项目信息成功Maintenance item information saved successfully。")
                    ResetForm(vue);
                } else {
                    toastr.error(res.message);
                }
            },
            error: function (err) {
                console.log(err);
                toastr.error("加载保养项目信息失败，请稍候再试Failed to load maintenance item information. Please try again later。");
            },
            complete: function () {
                parent.HideAzLoading();
                $("button").removeAttr("disabled");
            }
        });
    }
}

/**
 * 更新保养项目信息
 */
function UpdateMaintainItem(vue) {
    if (CheckMaintainItem(vue)) {
        parent.ShowAzLoading();
        $("button").attr("disabled", true);
        $.ajax({
            type: "POST",
            url: "/Maintain/UpdateMaintainItem",
            data: { item: vue.item },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("更新保养项目信息成功Maintenance item information updated successfully。")
                } else {
                    toastr.error(res.message);
                }
            },
            error: function (err) {
                console.log(err);
                toastr.error("加载保养项目信息失败，请稍候再试Failed to load maintenance item information. Please try again later。");
            },
            complete: function () {
                parent.HideAzLoading();
                $("button").removeAttr("disabled");
            }
        });
    }
}

/**
 * 检查保养项目信息正确性
 */
function CheckMaintainItem(vue) {
    if (!CheckMaintainItemName(vue)) {
        toastr.error("请填写正确保养项目内容Please fill in the correct maintenance items.。");
        return false;
    } else if (!CheckEquipmentType(vue)) {
        toastr.error("请填写选择设备类型Please fill in and select the equipment type。");
        return false;
    } else {
        return true;
    }
}

/**
 * 检查是否已填写保养项目内容
 */
function CheckMaintainItemName(vue) {
    $("#itemName").removeClass("has-error");
    if (vue.item.Name == null || vue.item.Name.length == 0) {
        $("#itemName").addClass("has-error");
        return false;
    }
    return true;
}

/**
 * 检查是否已选择设备类型
 */
function CheckEquipmentType(vue) {
    $("#equipmentType").removeClass("has-error");
    if (vue.item.EquipmentTypeID == 0) {
        $("#equipmentType").addClass("has-error");
        return false;
    }
    return true;
}

/**
 * 清空信息
 */
function ResetForm(vue) {
    vue.item = {
        Name: "",
        EquipmentTypeID: 0,
        IsDisable: 0
    };
}
